package edu.lingnan.lingnanblog.controller.admin;

import edu.lingnan.lingnanblog.entity.Blog;
import edu.lingnan.lingnanblog.entity.BlogType;
import edu.lingnan.lingnanblog.entity.PageBean;
import edu.lingnan.lingnanblog.service.BlogService;
import edu.lingnan.lingnanblog.service.BlogTypeService;
import edu.lingnan.lingnanblog.utils.BlogIndex;
import edu.lingnan.lingnanblog.utils.ResponseUtil;
import edu.lingnan.lingnanblog.utils.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 管理员博客Controller层
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/blog")
public class BlogAdminController {

	@Autowired
	private BlogService blogService;

	@RequestMapping("/add")
	public Integer add(Blog blog , BlogType blogType){
		blog.setBlogType(blogType);
		return blogService.add(blog);
	}
	@RequestMapping("/update")
	public Integer update(Blog blog, BlogType blogType){
		System.out.println("=========");
		System.out.println("blog = " + blog);
		System.out.println("=========");
		System.out.println(blog.getBlogType());
		blog.setBlogType(blogType);
		return blogService.update(blog);
	}

	/**
	 * 分页查询博客信息
	 */
	@RequestMapping("list")
	public List<Blog> list(Map<String, Object> map){
		return blogService.list(map);
	}
	@RequestMapping("getTotal")
	public Long getTotal(Map<String, Object> map){
		return blogService.getTotal(map);
	}

	/**
	 * 删除博客信息
	 */
	@RequestMapping("/delete")
	public Integer delete(@RequestParam(value="ids") int ids)throws Exception{
		return blogService.delete(ids);
	}

	/**
	 * 通过ID查找实体
	 */
	@RequestMapping("/getById")
	public Blog getById(@RequestParam(value="id")Integer id){
		return blogService.getById(id);
	}


	@Autowired
	BlogTypeService blogTypeService;

	@RequestMapping("toWriteBlog")
	public List<BlogType> toWriteBlog(){
		return blogTypeService.countList();
	}

	@RequestMapping("toModifyBlog")
	public List<BlogType> toModifyBlog(){
		return blogTypeService.countList();
	}

}